package com.didispace.control;

import com.didispace.domain.p.User;
import com.didispace.domain.p.User1;
import com.didispace.domain.p.UserRepository;
import com.didispace.domain.p.UserRepository1;
import com.didispace.domain.s.Message;
import com.didispace.domain.s.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
@EnableTransactionManagement
public class TestController {

    @Resource
    private UserRepository userRepository;
    @Resource
    private UserRepository1 userRepository1;
    @Resource
    private MessageRepository messageRepository;

    //@Autowired
    //TransactionInterceptor transactionInterceptor; 设置事物管理器


    @RequestMapping("/order")
    @Transactional(isolation = Isolation.DEFAULT ,propagation = Propagation.REQUIRED,value = "transactionManagerPrimary")
    public void test2() {
        System.out.println("begin.....");
        //当前中断执行，数据回滚失败
        messageRepository.save(new Message("o1", "aaaaaaaaaa"));
        messageRepository.save(new Message("o2", "bbbbbbbbbb"));
        messageRepository.save(new Message("o3", "cccccccccc"));
        //  Assert.assertEquals(3, messageRepository.findAll().size());
        userRepository.save(new User("aab", 10));
        userRepository.save(new User("bbb", 20));
        userRepository.save(new User("ccc", 30));
        userRepository.save(new User("ddd", 40));
        userRepository.save(new User("eee", 50));
        userRepository1.save(new User1("eee", 50));


        System.out.println("end.....");
    }

}
